package com.ramblingcoder.bc2.server.tool.update;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Socket;

import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.widget.RemoteViews;

import com.ramblingcoder.bc2.server.tool.update.R;
import com.ramblingcoder.bc2.server.tool.update.exceptions.InvalidArguments;
import com.ramblingcoder.bc2.server.tool.update.objects.Map;
import com.ramblingcoder.bc2.server.tool.update.objects.Server;

public class ServerWidget extends AppWidgetProvider {
	public static String ACTION_WIDGET_PREVIOUS = "PREVIOUS_SERVER";
	public static String ACTION_WIDGET_NEXT = "NEXT_SERVER";
	
	private static int serverIndex, counter;

	public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
		super.onUpdate(context, appWidgetManager, appWidgetIds);
		
		System.out.println("Rendering");
		
		if (counter == -1)
			return;
		
		RemoteViews view = new RemoteViews(context.getPackageName(), R.layout.widgetlayout);
		
		if (counter % 30 == 0){	//Update Info
			SharedPreferences settings = context.getSharedPreferences("BC2ServerTool", 0);
			String serverChoicesComplete = settings.getString("serverChoices", "");
			Server[] servers = Server.parseServer(serverChoicesComplete);

			if (servers.length != 0){
				if (serverIndex < 0) serverIndex = servers.length - 1;
				Server currentServer = servers[serverIndex % servers.length];
				
				try {
					Socket socket = new Socket();
					socket.connect(new InetSocketAddress(currentServer.getIp(), currentServer.getPort()), 250);
					
					String[] info = Server.getServerInfo(socket);
	
					view.setTextViewText(R.id.map, Map.findMap(info[4]).getExternalName());
					view.setTextViewText(R.id.players, info[1] + " / " + info[2]);
				} catch (IOException e) {
					view.setTextViewText(R.id.map, "Error Occured.");
					view.setTextViewText(R.id.players, "Unable to connect");
					e.printStackTrace();
				} catch (InvalidArguments e) {
					view.setTextViewText(R.id.map, "Error Occured.");
					view.setTextViewText(R.id.players, "Unable to connect");
					e.printStackTrace();
				} finally {
					view.setTextViewText(R.id.server, (currentServer.getName().trim().equals("") ? currentServer.getIp() : currentServer.getName()));
				}
			} else {
				view.setTextViewText(R.id.server, "No Saved Servers");
				view.setTextViewText(R.id.map, "Nothing to display.");
				view.setTextViewText(R.id.players, "");
			}

			counter = 0;
		}
		
		{
			Intent intent = new Intent(context, ServerWidget.class);
			intent.setAction(ACTION_WIDGET_NEXT);
			
			PendingIntent nextIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
			 
			view.setOnClickPendingIntent(R.id.next, nextIntent);		
		}

		{
			Intent intent = new Intent(context, ServerWidget.class);
			intent.setAction(ACTION_WIDGET_PREVIOUS);
			
			PendingIntent previousIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
			 
			view.setOnClickPendingIntent(R.id.previous, previousIntent);		
		}
		
		counter++;
		
		System.out.println("C");
		appWidgetManager.updateAppWidget(new ComponentName(context, ServerWidget.class), view);
	}
	
	public void onEnabled(Context context) {
		counter = 0;
	}
	
	public void onReceive(Context context, Intent intent) {
		System.out.println(intent.getAction());
		
		if (intent.getAction().equals(ACTION_WIDGET_PREVIOUS)) {
			System.out.println("A");
			serverIndex --;
			counter = 0;
		}
		
		if (intent.getAction().equals(ACTION_WIDGET_NEXT)) {
			System.out.println("B");
			serverIndex ++;
			counter = 0;
		}
		
		super.onReceive(context, intent);
	}
}
